/* CodeEval::SwapElements[Easy]
	Program should accept as its first argument a path to a filename.
	Input example is following:

		1 2 3 4 5 6 7 8 9 : 0-8
		1 2 3 4 5 6 7 8 9 10 : 0-1, 1-3

	Colon separates numbers with positions (starting at index 0)
	Process positions left to right

	Output:

		9 2 3 4 5 6 7 8 1
		2 4 3 1 5 6 7 8 9 10
	

	Solution by Michael Pio Arroyo

		Can use either array or linked list to store numbers
 */


#include <iostream>
#include <fstream>
#include <string>
#include <vector>

int main(int argc, char** argv) {
	std::ifstream file;
	std::string line;
	std::vector<int> list;
	
	file.open(argv[1]);

	if (file.is_open()) {
		int indexA, indexB;
		std::string temp;
		while (std::getline(file, line)) {
			bool swap = false;
			bool seta = true;
			bool bset = false;
			int listSize = 0;
			indexA = indexB = 0;
			for (std::string::iterator it = line.begin(); it < line.end(); it++) {
				if (!swap) {
					// store elements into number array
					if (*it != ':') {
						if (*it != ' ') {
							// add character into temp
							std::string t;
							t = *it;
							temp.append(t.c_str());
						}
						else {
							// convert temporary characters to integer
							int n = std::stoi(temp);
							// add to list
							list.push_back(n);
							temp.clear();
							listSize++;
						}
					}
					else {
						swap = true;
						temp.clear();
					}
				}
				else {
					// execute swap after number list is collected
					if (*it != ' ') {
						if (*it != '-' && *it != ',') {
							// determine swap value
							std::string t;
							t = *it;
							temp.append(t.c_str());
						}
						else {
							if (seta) {
								// store index a
								indexA = std::stoi(temp);
 								temp.clear();
								// switch to b flag
								seta = false;
							}
							else {
								// store index b
								indexB = std::stoi(temp);
								temp.clear();
								// swap values at index a and b
								int t = list.at(indexA);
								list.at(indexA) = list.at(indexB);
								list.at(indexB) = t;

								seta = true;
							}
						}
					} // endif *it != ' '
				} // endif !swap
			} // endfor
			// do last swap
			// swap values at index a and b
			indexB = std::stoi(temp);
			temp.clear();
			int t = list.at(indexA);
			list.at(indexA) = list.at(indexB);
			list.at(indexB) = t;

			for (int i = 0; i < listSize; i++)
				std::cout << list.at(i) << ' ';
			std::cout << std::endl;
			list.clear();
		}
	}
	else {
		std::cout << "file open error\n";
	}
	file.close();
	return 0;
}